package com.smday.sort;

import java.util.Arrays;

/**
 * @author Summerday
 */
public class BubbleSort {


    public static void main(String[] args) {
        int[] arr = {3, 9, -1, 10, 12};
        System.out.printf("排序前的数组:%s \n",Arrays.toString(arr));
        System.out.println("====================================");
        for (int i = 0; i < arr.length-1;i++){
            boolean flag = true;
            for(int j = 0;j<arr.length-1-i;j++){
                //交换
                if(arr[j]>arr[j+1]){

                    arr[j] ^= arr[j+1];
                    arr[j+1]^=arr[j];
                    arr[j]^=arr[j+1];
                    flag = false;
                }
            }
            System.out.printf("第%d趟排序后的结果:%s \n",i+1, Arrays.toString(arr));
            if(flag){
                System.out.println("此时已经完全排序,后面不需要再排");
                break;
            }

        }
    }


}
